package com.pai.service.Personnel;


import com.pai.dao.AppUser.AppUserDao;
import com.pai.dao.AppUser.AppUserDaoImpl;
import com.pai.dao.BaseDao;
import com.pai.dao.Personnel.PersonnelDao;
import com.pai.dao.Personnel.PersonnelDaoImpl;
import com.pai.entity.AppUser;
import com.pai.entity.Personnel;


import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class PersonnelServiceImpl implements PersonnelService {
    private PersonnelDao theDao;  //人员dao
    private AppUserDao appUserDao;  //系统用户dao

    public PersonnelServiceImpl() {

        this.theDao = new PersonnelDaoImpl();
        this.appUserDao = new AppUserDaoImpl();
    }

    /**
     * 获取列表
     * @return
     */
    @Override
    public List<Personnel> getList() {
        Connection connection = null;
        connection = BaseDao.getConnection();
        List<Personnel> resultList = new ArrayList<>();
        try {
            resultList = theDao.getList(connection);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return resultList;
    }

    /**
     * 获取列表
     * @param departmentId 部门id
     * @param depName  部门名称
     * @param perName  人员名称
     * @return
     */
    @Override
    public List<Personnel> getList(String departmentId, String depName, String perName) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        List<Personnel> resultList = new ArrayList<>();
        try {
            resultList = theDao.getList(connection, departmentId, depName, perName);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return resultList;
    }

    /**
     * 获取学生列表
     * @param parentDepId  二级学院id
     * @return
     */
    @Override
    public List<Personnel> getStudentList(String parentDepId) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        List<Personnel> resultList = new ArrayList<>();
        try {
            resultList = theDao.getStudentList(connection, parentDepId);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return resultList;
    }

    /**
     * 获取学生列表（辅导员）
     * @param colId  辅导员id
     * @return
     */
    @Override
    public List<Personnel> getStudentListCol(String colId) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        List<Personnel> resultList = new ArrayList<>();
        try {
            resultList = theDao.getStudentListCol(connection, colId);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return resultList;
    }

    /**
     * 获取项详情
     * @param id
     * @return
     */
    @Override
    public Personnel getItemDetail(String id) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        Personnel item = new Personnel();
        try {
            item = theDao.getItemDetail(connection, id);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return item;
    }
    /**
     * 新增
     * @param item
     * @return
     */
    @Override
    public boolean add(Personnel item) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        boolean flag = false;

        try {
            flag = theDao.add(connection, item) > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        BaseDao.closeResource(connection, null, null);
        return flag;
    }



    /**
     * 更新 用户的时候，同时也要更新账号
     * @param item
     * @return
     */
    @Override
    public boolean update(Personnel item) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        boolean flag = false;

        try {
            //人员信息更新时，可能会更新到工号，这样保证每次更新，工号和系统账号都能对得上（可能吧，这样做只是临时政策）
            flag = theDao.update(connection, item) > 0;
            String _appUserId = item.getAccountId()+"";
            String _newAccount = item.getWorkno();
            flag = appUserDao.updateAccount(connection, _appUserId, _newAccount) > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        BaseDao.closeResource(connection, null, null);
        return flag;
    }

    /**
     * 删除  也应该同时删去系统账号
     * @param id
     * @return
     */
    @Override
    public boolean delete(String id) {
        Connection connection = null;
        connection = BaseDao.getConnection();
        boolean flag = false;

        try {
            int _appUserId = theDao.getItemDetail(connection, id).getAccountId();
            flag = appUserDao.delete(connection, _appUserId+"") > 0;  //先删除系统账号
            flag = theDao.delete(connection, id) > 0;  //再删除人员信息
        } catch (Exception e) {
            e.printStackTrace();
        }
        BaseDao.closeResource(connection, null, null);
        return flag;
    }




}
